﻿using Sofia.Models;
using Sofia.Web.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Sofia.Web.Controllers
{
    public class ViewPostController : Controller
    {
        SofiaDb db = new SofiaDb();

        //
        // GET: /ViewPost/

        public ActionResult Index(int id)
        {
            if(!db.Storys.Any(x=>x.Id == id))
            {
                return Redirect("/");
            }

            var post = db.Storys.Where(x => x.Id == id)
                .Select(x => new StoryViewModel
                {
                     Title = x.Title,
                     Description = x.Description,
                     CreationTime = x.CreationTime,
                     userName = x.UserName.UserName,
                }).SingleOrDefault();
            return View(post);
        }

        public ActionResult Corp(int id)
        {
            if(!db.Corps.Any(x=>x.Id == id))
            {
                return Redirect("/");
            }
            var post = db.Corps.Where(x => x.Id == id)
                .Select(x => new CorpViewModel
                {
                    Title = x.Title,
                    Description = x.Description,
                    CreationTime = x.CreationTime,
                    username = x.userName.UserName,
                });
            return View(post);
        }

        public ActionResult Love(int id)
        {
            if(!db.Loves.Any(x=>x.Id == id))
            {
                return Redirect("/");
            }
            var post = db.Loves.Where(x => x.Id == id)
                .Select(x => new LoveViewModel
                {
                    Title = x.Title,
                    Description = x.Description,
                    CreationTime = x.CreationTime,
                    UserName = x.UserName.UserName,
                });
            return View(post);
        }

        public ActionResult SysAdmin(int id)
        {
            if(!db.SysAdmins.Any(x=>x.Id == id))
            {
                return Redirect("/");
            }

            var post = db.SysAdmins.Where(x => x.Id == id)
                .Select(x => new SysAdminViewModel
                {
                    Title = x.Title,
                    Description = x.Description,
                    CreationTime = x.CreationTime,
                    Username = x.UserName.UserName,
                });
            return View(post);
        }

        public ActionResult Program(int id)
        {
            if(!db.Programs.Any(x=>x.Id == id))
            {
                return Redirect("/");
            }

            var post = db.Programs.Where(x => x.Id == id)
                .Select(x => new programViewModel
                {
                    Title = x.Title,
                    Description = x.Description,
                    CreationTime = x.CreationTime,
                    username = x.UserName.UserName,
                });
            return View(post);
        }

        public ActionResult Manage(int id)
        {
            if(!db.Manages.Any(x=>x.Id == id))
            {
                return Redirect("/");
            }
            var post = db.Manages.Where(x => x.Id == id)
                .Select(x => new manageViewModel
                {
                    Title = x.Title,
                    Description = x.Description,
                    CreationTime = x.CreationTime,
                    userName = x.userName.UserName,
                });
            return View(post);
        }

        public ActionResult Market(int id)
        {
            if(!db.Markets.Any(x=>x.Id == id))
            {
                return Redirect("/");
            }

            var post = db.Markets.Where(x => x.Id == id)
                .Select(x => new MarketViewModel
                {
                    Title = x.Title,
                    CreationTime = x.CreationTime,
                    Description = x.Description,
                    userName = x.userName.UserName,
                });
            return View(post);
        }
    }
}
